package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.model.Board;
import com.conversationboard.model.Boards;
import com.conversationboard.model.User;

@WebServlet(name = "AdminRemoveUserFromClosedBoardControllerServlet", urlPatterns = "/Admin/AdminRemoveUserFromClosedBoardControllerServlet")
public class AdminRemoveUserFromClosedBoardControllerServlet extends HttpServlet {

	private static final long serialVersionUID = 2374325135596369939L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		try {
			int boardId = Integer.parseInt(request.getParameter("boardid"));
			String idToRemove = request.getParameter("member");
			User user = User.get(request.getUserPrincipal().getName());

			if (!user.isBoardAdmin(boardId)) {
				return;
			}

			if (!SecurityChecks.passes(request, user, this)) {
				return;
			}

			Board board = Boards.getBoard(boardId);
			board.removeUserFromClosedBoard(idToRemove);

			request.setAttribute("message", "User has been successfully remove from the board.");

			RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/messagepage.jsp");
			dispatcher.forward(request, response);

		} catch (NumberFormatException e) {
		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
